package com.ykdl.member.kid.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.ykdl.member.kid.R;

public abstract class  BaseFragment extends Fragment {
	
	

	protected RelativeLayout progress = null;
	protected RelativeLayout errorViewLayout = null;
	protected RelativeLayout centerViewLayout = null;
	protected TextView tv_error_text;
	protected View convertView  = null;
	protected ProgressBar loadingView = null;
	public Context mContext;
	private ImageView iv_error_pic;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		 convertView = inflater.inflate(R.layout.base_fragment_main, null);
		 mContext =  getActivity();
		 centerViewLayout = (RelativeLayout) convertView.findViewById(R.id.baseCenterView);
		 errorViewLayout = (RelativeLayout) convertView.findViewById(R.id.error_view);
		 tv_error_text = (TextView) convertView.findViewById(R.id.tv_error_text);
		 loadingView = (ProgressBar) convertView.findViewById(R.id.loading);
		 iv_error_pic = (ImageView) convertView.findViewById(R.id.iv_error_pic);
		 
		 iv_error_pic.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showErrorView(false, null);
				setIsShowLoad(true);
				tryAgain();
				
			}
		});
		 
		
		 onNewCreateView(convertView);
		
		return convertView;
	}

	
	 /**
     * 设置中间试图
     *
     * @param layoutId
     */
    protected void setCenterView(int layoutId) {
        View view = View.inflate(getActivity(), layoutId, null);
        setCenterView(view);
    }

    /**
     * 设置中间试图
     *
     * @param view
     */
    protected void setCenterView(View view) {
        setCenterView(view, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
    }

    /**
     * 设置中间试图
     *
     * @param view
     * @param params
     */
    protected void setCenterView(View view, ViewGroup.LayoutParams params) {
        centerViewLayout.addView(view, params);
    }

	protected abstract void onNewCreateView(View view);
	

	protected void setIsShowLoad(boolean flag) {
        if (flag) {
            loadingView.setVisibility(View.VISIBLE);
        } else {
            loadingView.setVisibility(View.GONE);
        }
    }
	
	protected void showErrorView(boolean flag,String content){
		if (flag) {
			errorViewLayout.setVisibility(View.VISIBLE);
			centerViewLayout.setVisibility(View.GONE);
			if (!TextUtils.isEmpty(content)) {
				
				tv_error_text.setText(content);
			}
		}else {
			errorViewLayout.setVisibility(View.GONE);
			centerViewLayout.setVisibility(View.VISIBLE);
		}
	}
	
	protected abstract void tryAgain();
	
	
	
}
